<script type="text/html" template lay-done="layui.data.sendParams(d.params)">
<div class="fromPage">
    <form class="layui-form layui-form-pane" lay-filter="LAY-system-role-permission">
        <div class="layui-btn-group">
            <button type="button" class="layui-btn layui-btn-sm" lay-roleOp="expandAll">展开</button>
            <button type="button" class="layui-btn layui-btn-sm layui-btn-normal" lay-roleOp="foldAll">折叠</button>
        </div>
        <div id="LAY-system-role-permission-tree" style="overflow: auto;height: 380px;margin: 15px;"></div>
    </form>
</div>
</script>
<script>
    layui.data.sendParams = function(params){
        const fromData = params.data;
        layui.use(['tree', 'baseMethods'], function (){
            let {tree, baseMethods} = layui, permissionData = [];
            tree.render({
                elem: '#LAY-system-role-permission-tree',
                data: permissionData,
                id: 'LAY-system-role-permission-treeId',
                showLine: false,
                showCheckbox: true,
                text: {defaultNodeName: '未命名', none: '无数据'},
            });
            baseMethods.request({
                url: '/system/role/' + fromData.id + '/permission',
                done: function (result){
                    if (result.code === 200){
                        let data = result.data;
                        permissionData = setData(data.list, data.checked);
                        tree.reload('LAY-system-role-permission-treeId', {data: permissionData});
                    }
                }
            });
            layui.util.event('lay-roleOp', {
                expandAll: function (){
                    permissionData = setDataFolding(permissionData, 'expandAll');
                    tree.reload('LAY-system-role-permission-treeId', {data: permissionData});
                },
                foldAll: function (){
                    permissionData = setDataFolding(permissionData, 'foldAll');
                    tree.reload('LAY-system-role-permission-treeId', {data: permissionData});
                },
            });
            function setData(list, checked){
                if (list && list.length === 0) return [];
                return list.map(function (item){
                    let arr = {
                        id: item.name,
                        title: item.display_name,
                        field: 'permissions[]',
                        checked: checked.includes(item.name),
                    }
                    if (item.children.length > 0){
                        //arr.spread = arr.checked;
                        arr.children = setData(item.children, checked);
                    }
                    return arr;
                });
            }
            function setDataFolding(list = [], type = ''){
                if (list && list.length === 0) return [];
                for (const i in list){
                    if (type === 'expandAll'){
                        list[i].spread = true;
                    } else if (type === 'foldAll'){
                        list[i].spread = false;
                    }
                    if (list[i].children && list[i].children.length > 0){
                        list[i].children = setDataFolding(list[i].children, type);
                    }
                }
                return list;
            }
        })
    }
</script>
